<?php
/**
 * Medical Center Header Block
 *
 * @author Xavier VILAIN
 * @package Nawa
 * @license
 */

use Fox\Message;
use Fox\Generator;
use Fox\Msg;
use Fox\Html\Header\Image;
use Nawa\Messages;
use Fox\Dao\SqlFragment;
use Nawa\MedicalCenter;
use Fox\Html\Screen;
$xview = Xview::instance();
$screen = new Screen('sMedicalCenter','create');
$screen->mainTitle(new Image("medicalcenter","<div class=\"page-title\">".Msg(Messages::BUNDLE,Messages::LABEL_MEDICAL_CENTER_CREATE)."</div>"));

// Header
$blHead = $xview->component("medicalcenters","head");
$screen->add($blHead);
// Addresse Data
$blAddress = Xview::instance()->component("customers", "address");
$screen->add($blAddress);

if($screen->submitted())
{
	$fields = $screen->get();
	if(!$screen->errors())
	{
		try {
			$medicalCenter = new MedicalCenter();
			$medicalCenter->setFields($fields);
			$medicalCenter->create();
			Fox\Message::Success(Msg(Msg::BUNDLE,Msg::MSG_CREATE,$fields['name1']));
			$screen->redirect(Generator::url('medicalcenters','update',null,
			array('mcid' => $medicalCenter->getId())));
		}
		catch(Fox\Object\Exception\Save $e)
		{
			if($e->reason ==  MedicalCenter::CORE_ERROR_REASON_DUPLICATES)
			{
				Message::Error(Msg(Msg::BUNDLE,Msg::ERR_DUPLICATE,$fields['name1']));
			}
			else
			{
				Message::Error($e->getMessage());
			}
		}
	}
}
echo $screen->display();